package leetcode.editor.cn;

import java.util.Stack;
import java.util.StringTokenizer;

//Java：删除字符串中的所有相邻重复项
public class RemoveAllAdjacentDuplicatesInString {
    public static void main(String[] args) {
        Solution solution = new RemoveAllAdjacentDuplicatesInString().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String removeDuplicates(String s) {
            char[] charArray = s.toCharArray();

            Stack<Character> stack = new Stack<>();

            for (int i = 0; i < charArray.length; i++) {
                if (stack.empty() || stack.peek() != charArray[i]) {
                    stack.push(charArray[i]);
                } else {
                    stack.pop();
                }
            }
            String result = new String();
            while (!stack.empty()) {
                result = stack.pop() + result;
            }
            return result;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}